﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace DevEx
{
    class CLoaiSanPham
    {
        private static QLVTDataContext GetDc()
        {
            QLVTDataContext dc = new QLVTDataContext();
            return dc;
        }

        public IQueryable<tblLoaiSanPham> SelectAllRecord()
        {
            QLVTDataContext dc = GetDc();

            var q = from p in dc.tblLoaiSanPhams
                    select p;
            return q;
        }

        public tblLoaiSanPham SelectRecordByID(int ID_LoaiSanPham)
        {
            QLVTDataContext dc = GetDc();

            var q = (from p in dc.tblLoaiSanPhams where p.ID_LoaiSanPham == ID_LoaiSanPham select p).SingleOrDefault();
            return q;
        }

        //Insert
        public void Insert(tblLoaiSanPham obj)
        {
            QLVTDataContext dc = GetDc();
            dc.tblLoaiSanPhams.InsertOnSubmit(obj);
            dc.SubmitChanges();
        }
        //Delete
        public void Delete(int ID)
        {
            QLVTDataContext dc = GetDc();

            var kho = dc.tblLoaiSanPhams.SingleOrDefault(p => p.ID_LoaiSanPham == ID);
            dc.tblLoaiSanPhams.DeleteOnSubmit(kho);
            dc.SubmitChanges();
        }
        //Update
        public void Update(tblLoaiSanPham obj)
        {
            QLVTDataContext dc = GetDc();
            dc.tblLoaiSanPhams.Attach(obj);
            dc.Refresh(RefreshMode.KeepCurrentValues, obj);
            dc.SubmitChanges(ConflictMode.ContinueOnConflict);
        }
    }
}
